#!/bin/bash 
cd ../
#The device name and the mountpoint are provided as parameters.
_device=/dev/mapper/testvg-testlvm
_mntpt=/tmp/mnt
_loop1=/dev/loop1
_loop2=/dev/loop2

#Just unset this if you don't have sudo configured or directly
#running as root
SUDO=sudo

#
# Cleanup 
#

umount $_device $_mntpt -f &> /dev/null
rmmod ohsm &> /dev/null
vgremove -f testvg &> /dev/null
rm -rf ext2image* &> /dev/null
rm -rf _temp* &> /dev/null
rm -rf $_mntpt

#
# Prepare the build first
#

printf "Configuring OHSM......................"
$SUDO ./configure &> /dev/null
if [ $? != 0 ]; then
        rc=$?
        echo "Failed"
	echo "Error in configure............. Error =  $rc "
	exit 1
else
        echo "Passed\n"
fi

$SUDO make clean &> /dev/null

$SUDO make &> /dev/null
if [ $? != 0 ]; then
	echo "Error in make......... Error =  $? "
	exit 1
fi

$SUDO mkdir -p $_mntpt &> /dev/null

dd if=/dev/zero of=./ext2image1 seek=500000 count=0  &> /dev/null
dd if=/dev/zero of=./ext2image2 seek=500000 count=0  &> /dev/null

#Setup loopback devices
$SUDO modprobe loop &> /dev/null
losetup -d $_loop1 &>/dev/null
losetup -d $_loop2 &>/dev/null

losetup $_loop1 ./ext2image1 &> /dev/null
losetup $_loop2 ./ext2image2 &> /dev/null

pvcreate $_loop1 $_loop2 &> /dev/null

#create volume group
vgremove testvg &> /dev/null
vgcreate testvg $_loop1 $_loop2 &> /dev/null
 
#Display the volume group
vgdisplay testvg &> /dev/null

#Create the LVM
lvcreate -n testlvm -L 400M testvg &> /dev/null
mke2fs /dev/testvg/testlvm &> /dev/null

#Creating the file system and mounting the filesystem.
	
mount $_device $_mntpt &> /dev/null

if [ $? != 0 ]; then
	echo "Error Mounting $_device on $_mntpt: Error =   $? "
	exit 1
fi

#Inserting the OHSM kernel driver
insmod kernel/ohsm.ko ohsm_enable_debug=5 &> /dev/null

if [ $? != 0 ]; then
	echo "Error insering module: Error =  $? "
	exit 1
fi

major=`grep OHSM_KERNEL_DRIVER /proc/devices | awk '{print $1}'`

[ -z "$major" ] && echo "Unable to get major number" && exit 1
rm -rf /dev/ohsm &> /dev/null
mknod /dev/ohsm c $major 0

if [ $? != 0 ]; then
	echo "Error creating device special file /dev/ohsm: Error = " $?
	exit 1
fi

	echo "********************************************"
	echo "*      OHSM Command Conformance test       *"
	echo "********************************************"

#
# Enabling OHSM
#
$SUDO userspace/ohsm -e -w $_device -x sample/device.xml -y sample/alloc.xml -z sample/reloc.xml -m $_mntpt &> _temp 
grep "Enabled Successfully" _temp &> /dev/null

if [ $? != 0 ]; then
	echo "OHSM enabling.........................Failed"
	exit 1
else
	echo "OHSM enabling.........................Passed" 
fi


#
# STATUS OHSM
#

$SUDO userspace/ohsm -n &> _temp
grep "enabled" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM Status check after enabling......Passed" 
else
	echo "OHSM Status check after enabling......Failed" 
fi


#
# Disable OHSM 
#

$SUDO userspace/ohsm -d &> _temp
grep "disabled successfully" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM disabling........................Passed" 
else
	echo "OHSM disabling........................Failed" 
	exit 1
fi


#
# STATUS OHSM
#

$SUDO userspace/ohsm -n &> _temp
grep "disabled" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM Status check after disabling.....Passed" 
else
	echo "OHSM Status check after disabling.....Failed" 
	exit 1
fi


#
# Re Disable OHSM 
#

$SUDO userspace/ohsm -d &> _temp
grep "already disabled" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM re-disabling.....................Passed" 
else
	echo "OHSM re-disabling.....................Failed" 
	exit 1
fi


#
# Re-enabling OHSM when it is already enabled
#

$SUDO userspace/ohsm -e -w $_device -x sample/device.xml -y sample/alloc.xml -z sample/reloc.xml -m $_mntpt &> _temp 
$SUDO userspace/ohsm -e -w $_device -x sample/device.xml -y sample/alloc.xml -z sample/reloc.xml -m $_mntpt &> _temp 
grep "File exists" _temp &> /dev/null

if [ $? != 0 ]; then
	echo "OHSM re-enabling......................Failed"
	exit 1
else
	echo "OHSM re-enabling......................Passed" 
fi


#
# OHSM help
#

$SUDO userspace/ohsm -h &> _temp
grep "Usage" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM help.............................Passed" 
else
	echo "OHSM help.............................Failed" 
	exit 1
fi


#
# OHSM Dipslaying SAM table
#

$SUDO userspace/ohsm -v -t &> _temp
grep "SAM" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM displaying SAM table.............Passed" 
else
	echo "OHSM displaying SAM table.............Failed" 
	exit 1
fi


#
# OHSM Dipslaying files for debug 
#

$SUDO userspace/ohsm -v -f &> _temp
grep "Use dmesg to see results" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM displaying files for debug.......Passed" 
else
	echo "OHSM displaying files for debug.......Failed" 
	exit 1
fi


#
# OHSM  Get the tier device map 
#

$SUDO userspace/ohsm -g -i &> _temp
grep "Tier Device map" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM get tier device map..............Passed" 
else
	echo "OHSM get tier device map..............Failed" 
	exit 1
fi


#
# OHSM  Get the Alloc policy 
#

$SUDO userspace/ohsm -g -a &> _temp
grep "allocation policy information" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM get allocation policy............Passed" 
else
	echo "OHSM get allocation policy............Failed" 
	exit 1
fi


#
# OHSM  Get the Relocation policy 
#

$SUDO userspace/ohsm -g -p &> _temp
grep "relocation policy" _temp &> /dev/null

if [ $? == 0 ]; then
	echo "OHSM get relocation policy............Passed" 
else
	echo "OHSM get relocation policy............Failed" 
	exit 1
fi

$SUDO umount $_device $_mntpt -f &> /dev/null
$SUDO rmmod ohsm &> /dev/null
$SUDO vgremove -f testvg &> /dev/null
$SUDO losetup -d $_loop1 &> /dev/null
$SUDO losetup -d $_loop2 &> /dev/null
rm -rf ext2image* &> /dev/null
rm -rf _temp* &> /dev/null
rm -rf $_mntpt &> /dev/null 
